import type {App} from 'vue';
import type {I18nOptions} from 'vue-i18n';
import {createI18n} from 'vue-i18n';
import {setHtmlPageLang, setLoadLocalePool} from './helper';
import {useLocaleStore} from '@/models/modules/locale';

// import zhCN from './lang/zh_CN';
// import en from './lang/en';

export let i18n: ReturnType<typeof createI18n>;

async function createI18nOptions(): Promise<I18nOptions> {
  const LocaleModel = useLocaleStore();
  const locale = LocaleModel.getLocale;
  const defaultLocal = await import(`./lang/${locale}.ts`);
  const message = defaultLocal.default?.message ?? {};

  setHtmlPageLang(locale);
  setLoadLocalePool((loadLocalePool) => {
    loadLocalePool.push(locale);
  });
  // console.log('msg', message);
  return {
    legacy: false,
    locale: locale || 'zh_CN', // 选中的语言
    fallbackLocale: 'zh_CN', // 没有英文的时候默认中文语言
    messages: {
      [locale]: message,
    },
    // messages: {
    //   zh_CN: zhCN.message || {},
    //   en: en.message || {},
    // },
    sync: true, //如果您不想从全局范围继承区域设置，则需要将i18n组件选项的sync设置为false。
    silentTranslationWarn: true, // true - warning off //抑制警告
    missingWarn: false,
    silentFallbackWarn: true,
  };
}

export async function setupI18n(app: App) {
  const options = await createI18nOptions();
  i18n = createI18n(options);
  app.use(i18n);
}
